from django.shortcuts import render
from django.contrib.auth.hashers import make_password, check_password
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db.models import Avg,Min,Sum,Max,Count,F,Q
from django.db import connection
from myapp.myserializers import *
from myapp.models import *
from django.core.paginator import Paginator

# Create your views here.

# 用户展示接口
class UserListAPIView(APIView):
    def get(self, requets):
        user_list = User.objects.all()
        user_ser = UserSerializer(user_list, many=True)
        return Response({
            'code':200,
            'data':user_ser.data
        })

# 升级VIP接口
class VIPAPIView(APIView):
    def get(self, request):
        id = request.GET.get('id')
        print(id)
        user = User.objects.filter(pk=int(id)).first()
        user.huiyuan = 1
        user.save()
        YouHuiJuan.objects.create(man=20, jian=10, userid_id=user.id)
        YouHuiJuan.objects.create(man=10, jian=5, userid_id=user.id)
        return Response({
            'code':200,
            'data':'升级成功'
        })

# 图书展示接口
class BookListAPIView(APIView):
    def get(self, request):
        book_list = Books.objects.all()
        book_ser = BookSerializer(book_list, many=True)
        return Response({
            'code':200,
            'data':book_ser.data
        })

# 购买接口
class GouMaiAPIView(APIView):
    def get(self, request):
        username = request.GET.get('username')
        id = request.GET.get('id')
        user = User.objects.filter(username=username).first()
        book = Books.objects.filter(pk=int(id)).first()
        if book.count <=0:
            return Response({
                'code':300,
                'data':'抱歉，库存不足'
            })

        else:
            book.count = book.count - 1
            GouWuChe.objects.create(price=book.price, bookid_id=book.id,userid_id=user.id)
            return Response({
                'code':200,
                'data':'购买成功'
            })


# 登陆接口
class LoginAPIView(APIView):
    def get(self, request):
        username = request.GET.get('username')
        password = request.GET.get('password')
        print(username)
        print(password)
        user = User.objects.filter(username=username, password=password).first()
        if user:
            return Response({
                'code':200,
                'data':'登陆成功'
            })
        else:
            return Response({
                'code':300,
                'data':'账号或密码错误'
            })


# 购物车展示接口
class GouWuCheAPIView(APIView):
    def get(self, request):
        username = request.GET.get('username')
        user = User.objects.filter(username=username).first()
        gouwuche_list = GouWuChe.objects.filter(userid_id=user.id)
        gouwuche_ser = GouSerializer(gouwuche_list, many=True)
        num = 0
        for i in gouwuche_list:
            num += i.price

        return Response({
            'code':200,
            'data':gouwuche_ser.data,
            'z_prict':num
        })

# 优惠卷展示接口
class YouHuiAPIView(APIView):
    def get(self, request):
        username = request.GET.get('username')
        user = User.objects.filter(username=username).first()
        youhui_list = YouHuiJuan.objects.filter(userid_id=user.id)
        youhui_ser = YouSerializer(youhui_list, many=True)
        return Response({
            'code':200,
            'data':youhui_ser.data
        })



